package com.kun.portal.webframe.controller;

import com.kun.portal.webframe.po.FlyMessagePo;
import com.kun.portal.webframe.service.FlyMessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

/**
 * Created by Administrator on 2018/5/18.
 */
@Controller
@RequestMapping("/api/flyRequest")
public class FlyMessageController {

    @InitBinder
    public void intDate(WebDataBinder dataBinder){
        dataBinder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
    }
    @Autowired
    private FlyMessageService flyMessageService;
    @RequestMapping(value="/upload",method= RequestMethod.POST)
    @ResponseBody
    public FlyMessagePo upload(HttpServletRequest request, @RequestParam(required = false) MultipartFile file) throws Exception {
        Enumeration<String> list= request.getParameterNames();
        FlyMessagePo flyMessagePo =  new FlyMessagePo();
        flyMessagePo.setDescription(request.getParameter("description"));
        flyMessagePo.setMessageType(request.getParameter("messageType"));
        flyMessagePo.setStatus(request.getParameter("status"));
        String messageDateS = request.getParameter("messageDate");
        String messageId = request.getParameter("messageId");
        String uploadFileType =  request.getParameter("uploadFileType");
        String name = request.getParameter("name");
        String bakName = request.getParameter("bakName");
        flyMessagePo.setBakName(bakName);
        flyMessagePo.setName(name);
        flyMessagePo.setUrl(request.getParameter("url"));
        flyMessagePo.setBakUrl(request.getParameter("bakUrl"));
        if(StringUtils.isNoneBlank(messageId)){
            flyMessagePo.setMessageId(messageId);
        }
        flyMessagePo.setUploadDate(new Date());
        if(StringUtils.isNoneBlank(messageDateS)){
            SimpleDateFormat dateFormat =  new SimpleDateFormat("yyyy-MM-dd");
            Date messageDate=dateFormat.parse(messageDateS);
            flyMessagePo.setMessageDate(messageDate);
        }
        //如果文件不为空，写入上传路径
        if(!file.isEmpty()) {
            //上传文件路径
            String path = request.getServletContext().getRealPath("");
            //上传文件名
            path+="../flyMessage"+flyMessagePo.getMessageType();
            String filename = file.getOriginalFilename();

            File filepath = new File(path,filename);
            //判断路径是否存在，如果不存在就创建一个
            if (!filepath.getParentFile().exists()) {
                filepath.getParentFile().mkdirs();
            }
            //将上传文件保存到一个目标文件当中
            file.transferTo(new File(path + File.separator + filename));
            if(uploadFileType.equals("bak")){
                flyMessagePo.setMessageId(messageId);
                flyMessagePo.setBakName(filename);
                flyMessagePo.setBakUrl("/flyMessage"+flyMessagePo.getMessageType()+"/"+filename);
            }else{
                flyMessagePo.setName(filename);
                flyMessagePo.setUrl("/flyMessage"+flyMessagePo.getMessageType()+"/"+filename);
            }
            flyMessageService.save(flyMessagePo);
            return flyMessagePo;
        } else {

            return flyMessagePo;
        }

    }

    @RequestMapping(value="/update",method= RequestMethod.POST)
    @ResponseBody
    public FlyMessagePo save( @RequestBody FlyMessagePo flyMessagePo){
         return flyMessageService.save(flyMessagePo);
    }
    @RequestMapping(
            value = {"/query"},
            method = RequestMethod.GET
    )
    @ResponseBody
    public Page<FlyMessagePo> query(@RequestParam (required = false) String filter, Pageable pageable, Date startDate, Date endDate,String status){
        return flyMessageService.query(filter,pageable,startDate,endDate, status);
    }
}
